package com.ittest.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyTest {

    public static void main(String[] args) {
        Target target = new Target();
        Advice advice = new Advice();

        TargetInterface proxy = (TargetInterface) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler(){
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

                        //前置增强
                        advice.before();
                        Object invoke = method.invoke(target,args);
                        //后置增强
                        advice.afterReturning();
                        return invoke;
                    }
                }
        );

        proxy.save();
    }
}
